package com.xyu.controller;

import com.xyu.po.User;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/api/users")
public class UserController {

    private HashMap<Long, User> userHashMap= new HashMap<>();

    private AtomicLong idCounter = new AtomicLong(1); // 自增ID生成器

    @GetMapping
    public List<User> getAllUsers() {
        return new ArrayList<>(userHashMap.values());
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userHashMap.get(id);
    }

    @PostMapping
    public User saveUser(@RequestBody User user) {
        Long newId = idCounter.getAndIncrement();
        user.setId(newId);
        userHashMap.put(newId, user);
        return userHashMap.put(newId,user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userHashMap.remove(id);
    }
}    